/*
 * @Author: error: git config user.name & please set dead value or install git
 * @Date: 2022-08-31 10:26:21
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-11-20 14:02:53
 * @FilePath: \stm32g431_AllRings\applications\epg_xx.h
 * @Description:
 *
 * Copyright (c) 2022 by error: git config user.name && git config user.email & please set dead value or install git, All Rights Reserved.
 */
#ifndef __EPG_40_H
#define __EPG_40_H

#include <rtthread.h>
#include <board.h>
#include "rtdebug.h"
#include "allrings_sys.h"
// #include "my_fdcan_drv.h"

// /**************************EPG40 电动手寄存器表***********************/
// 状态寄存器-04
#define EPG_XX_STA_ADDR 0X07D0 // 状态寄存器首地址
// 0x07D0 低字节 电动夹爪状态寄存器
// 0x07D0 高字节
// 0x07D1 低字节 故障错误状态寄存器
// 0x07D1 高字节 位置状态寄存器
// 0x07D2 低字节 速度状态寄存器
// 0x07D2 高字节 力状态(即时电流)寄存器
// 0x07D3 低字节 母线电压寄存器
// 0x07D3 高字节 环境温度寄存器

// 保持寄存器-10
#define EPG_XX_CTR_ADDR 0X03E8 // 控制寄存器地址
// 0X03E8 低字节 控制寄存器
// 0X03E8 高字节 无参数控制指令寄存器

#define EPG_XX_CTR_3RING 0X03E9 // 三环设置寄存器，位置、速度、力矩
// 0x03E9 低字节 保留
// 0x03E9 高字节 位置设置寄存器
// 0x03EA 低字节 速度设置寄存器
// 0x03EA 高字节 力设置寄存器

#define EPG_XX_CTR_PRESET1 0X03EB // 预设参数1
#define EPG_XX_CTR_PRESET2 0X03ED // 预设参数2
#define EPG_XX_CTR_PRESET3 0X03EF // 预设参数3
#define EPG_XX_CTR_PRESET4 0X03F1 // 预设参数4
#define EPG_XX_CTR_PRESET5 0X03F3 // 预设参数5
#define EPG_XX_CTR_PRESET6 0X03F5 // 预设参数6
#define EPG_XX_CTR_PRESET7 0X03F7 // 预设参数7
#define EPG_XX_CTR_PRESET8 0X03F9 // 预设参数8

#define EPG_STA_BIT_ENABLE (0X01 << 0)
#define EPG_STA_BIT_MODE 2u      //(0X01 << 2)
#define EPG_STA_BIT_ACTION 3u    //(0X01 << 3)
#define EPG_STA_BIT_GOLDCHECK 6u // 0XC0

// 系统故障宏定义
#define EPG_ERR_NO_SET_ACTIVATE (1 << 7) // 未设置激活位
#define EPG_ERR_CTR_CMD (1 << 6)         // 控制指令错误
#define EPG_ERR_NO_COMM (1 << 5)         // 长时间无通讯
#define EPG_ERR_VOL (1 << 4)             // 电压异常
#define EPG_ERR_CUR (1 << 3)             // 电流异常
#define EPG_ERR_ENABLE (1 << 2)          // 使能故障
#define EPG_ERR_OVER_TEMP (1 << 1)       // 过温
#define EPG_ERR_ITSELF (1 << 0)          // 产品自身故障，返厂

// /*********************************************************************/
#define EPG_XX_CMD_LIST_TAB                    \
    /*读状态寄存器*/                     \
    X_MACRO(_EPG40_R_STA_ENABLE, 0X20)         \
    X_MACRO(_EPG40_R_STA_WORKMODE, 0X21)       \
    X_MACRO(_EPG40_R_STA_ACTION, 0X22)         \
    X_MACRO(_EPG40_R_STA_GOLDCHECK, 0X23)      \
    X_MACRO(_EPG40_R_STA_PREVALUES, 0X41)      \
    X_MACRO(_EPG40_R_STA_3RINGS, 0X42)         \
    X_MACRO(_EPG40_R_STA_VOL, 0X43)            \
    X_MACRO(_EPG40_R_STA_TEMP, 0X44)           \
    /*写控制寄存器*/                     \
    X_MACRO(_EPG40_W_ACTION_WITH_VALUES, 0X60) \
    X_MACRO(_EPG40_W_ACTION_NO_VALUES, 0X61)   \
    X_MACRO(_EPG40_W_PRE_SET_VALUES, 0X62)

typedef enum
{
#define X_MACRO(a, b) a = b,
    EPG_XX_CMD_LIST_TAB
#undef X_MACRO
} epg_xx_cmd_tab;

struct _EPG_COM_ERR
{
    uint8_t rs485_err_timeout_counter; // 超时次数计数
    uint8_t rs485_err_failure_counter; // 失败次数计数
};

// 状态寄存器数据
struct _EPG_STA_REG
{
    uint8_t epg_sta;      // EPG状态寄存器
    uint8_t epg_err;      // EPG系统故障
    uint8_t epg_position; // EPG位置
    uint8_t epg_speed;    // EPG速度
    uint8_t epg_current;  // EPG力矩
    uint8_t epg_temp;     // EPG系统温度
    uint8_t epg_voltage;  // EPG母线电压
};

union epg_prev
{
    struct byte_s
    {
        uint8_t epg_p;       // 预设位置
        uint8_t epg_s;       // 预设速度
        uint8_t epg_c;       // 预设力
        uint8_t epg_reserve; // 保留参数
    } bytes;

    uint16_t epgv[2];
};

struct _EPG_CTR_REG
{
    uint8_t epg_ctr;                  // EPG控制寄存器
    uint8_t epg_nov_ctr;              // EPG无参数控制指令
    uint8_t epg_s_p;                  // EPG位置设置参数
    uint8_t epg_s_s;                  // EPG速度设置参数
    uint8_t epg_s_c;                  // EPG力设置参数
    union epg_prev epg_pre_values[8]; // 8组预设参数
};

struct _EPG_ENABLE
{
    uint8_t epg_enable_auto_finished; // 自动初始化流程完成
    uint8_t epg_enable_times;         // 使能次数
    uint8_t epg_enable_err_temp;      // 临时错误状态
};

struct _EPG_VALUES
{
    uint8_t addr;
    uint16_t main_fsm_num; // 主线程状态机代号
    struct _EPG_ENABLE epg_enable_values;
    struct _EPG_COM_ERR epg_com_err_judge;
    struct _EPG_STA_REG epg_sta_reg_values;
    struct _EPG_CTR_REG epg_keep_reg_values;
};

struct _epg_ctr
{
    struct _EPG_VALUES epg_values;
    struct _fdcan_rxdata fdcan_fdback;                    // CAN原始数据结构体
    uint8_t (*device_cmd_epg[256])(struct _epg_ctr *epg); // EPG_XX设备命令接口
    uint8_t (*inputcmd_valid_check)(uint8_t cmdcheck);
    void (*can_sendback_data)(struct _fdcan_rxdata *fdcandata);
};

extern struct _epg_ctr epg_xx;

void epg_xx_main_ctr(struct _epg_ctr *epg);
// /*****************************指令接口函数*************************/
uint8_t EPG_XX_R_INPUTREG_KEEP(struct _epg_ctr *epg);
uint8_t EPG_XX_R_INPUTREG_STA(struct _epg_ctr *epg);

/*读参数*/
uint8_t EPG_XX_R_ENABLE(struct _epg_ctr *epg);
uint8_t EPG_XX_R_WORKMODE(struct _epg_ctr *epg);
uint8_t EPG_XX_R_ACTION(struct _epg_ctr *epg);
uint8_t EPG_XX_R_GOLDCHECK(struct _epg_ctr *epg);

uint8_t EPG_XX_R_PREVALUES(struct _epg_ctr *epg);
uint8_t EPG_XX_R_3RING(struct _epg_ctr *epg);
uint8_t EPG_XX_R_TEMP(struct _epg_ctr *epg);
uint8_t EPG_XX_R_VOL(struct _epg_ctr *epg);

/*写参数*/
uint8_t EPG_XX_W_CTREG_CTR(struct _epg_ctr *epg, uint8_t ctr_flag);
uint8_t EPG_XX_W_CTREG_WORK_VALUES(struct _epg_ctr *epg);
uint8_t EPG_XX_W_CTREG_WORK_NOVALUES(struct _epg_ctr *epg);
uint8_t EPG_XX_W_CTREG_PRESET_VALUE(struct _epg_ctr *epg);

// static 函数
static uint8_t epg_cmd_check(uint8_t cmdcheck);
static void epg_fdcan_send2up_data(struct _fdcan_rxdata *fdcandata);
static void epg_can_packed(struct _epg_ctr *epg, uint8_t *_fed_data, uint8_t _fed_length);
#endif
